package com.eagle.util;

import com.eagle.constant.Constant;
import com.eagle.entity.MavenDependency;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Notice:  only internal use
 *
 * @author qinlinsen
 */
public class StringUtil {

    public static MavenDependency toMavenDependencyFrom(final String mavenDependencyStr) {
        MavenDependency mavenDependency = new MavenDependency();
        boolean valid = isValidMavenDependencyStr(mavenDependencyStr);
        if (!valid) {
            throw new RuntimeException(String.format("%s is not valid maven dependency string," +
                    " the valid pattern is like groupId:artifactId:jar:version:xxx", mavenDependencyStr));
        }

        final String processedMavenDependencyStr = stripUnnecessaryCharacterWithin(mavenDependencyStr);
        String[] parts = processedMavenDependencyStr.split(Constant.MAVEN_DEPENDENCY_SEPARATOR);
        mavenDependency.setGroupId(parts[0]);
        mavenDependency.setArtifactId(parts[1]);
        mavenDependency.setVersion(parts[3]);
        return mavenDependency;
    }


    @Deprecated
    public static Map<String, String> toMapFrom(final String mavenDependencyStr) {
        HashMap<String, String> result = new HashMap<>();
        boolean valid = isValidMavenDependencyStr(mavenDependencyStr);
        if (!valid) {
            throw new RuntimeException(String.format("%s is not valid maven dependency string," +
                    " the valid pattern is like groupId:artifactId:jar:version:xxx", mavenDependencyStr));
        }

        final String processedMavenDependencyStr = stripUnnecessaryCharacterWithin(mavenDependencyStr);
        String[] parts = processedMavenDependencyStr.split(Constant.MAVEN_DEPENDENCY_SEPARATOR);
        result.put(Constant.GROUP_ID, parts[0]);
        result.put(Constant.ARTIFACT_ID, parts[1]);
        result.put(Constant.VERSION, parts[3]);
        return result;
    }


    public static boolean isValidMavenDependencyStr(final String mavenDependencyStr) {
        Objects.requireNonNull(mavenDependencyStr);
        if (!mavenDependencyStr.contains(Constant.MAVEN_DEPENDENCY_SEPARATOR)) {
            return false;
        }
        String[] parts = mavenDependencyStr.split(Constant.MAVEN_DEPENDENCY_SEPARATOR);

        if (parts.length != Constant.MAVEN_DEPENDENCY_STR_LENGTH) {
            return false;
        }

        final String finalPart = parts[parts.length - 1];
        if ((!finalPart.endsWith("compile"))
                && (!finalPart.endsWith("runtime"))
                && (!finalPart.endsWith("test"))) {
            return false;
        }

        return true;
    }

    private static String stripUnnecessaryCharacterWithin(final String target) {
        Objects.requireNonNull(target);
        final String result = target
                .replace(Constant.MAVEN_INFO, Constant.EMPTY_STRING)
                .replace(Constant.ADD_SUBTRACT, Constant.EMPTY_STRING)
                .replace(Constant.BACKSLASH_SUBTRACT, Constant.EMPTY_STRING)
                .replace(Constant.VERTICAL_LINE, Constant.EMPTY_STRING)
                .trim();
        return result;
    }


    public static void main(String[] args) {
        String s = "[INFO] |  |  \\- com.fasterxml:classmate:jar:1.3.3:compile";
        String s2 = "[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.4.4.RELEASE:compile";
        String s3="[INFO]    \\- org.hamcrest:hamcrest-core:jar:1.1:test";
        String s4="[INFO] +- com.google.guava:guava:jar:17.0:compile";
        String s5="[INFO]    |  +- org.codehaus.jettison:jettison:jar:1.3.7:runtime";
        String s6="[INFO] com.thoughtmechanix:configurationserver:jar:0.0.1-SNAPSHOT";
        final boolean valid = isValidMavenDependencyStr(s6);
        System.out.println(valid);
    }
}
